﻿using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Brushes;
using Microsoft.Graphics.Canvas.Effects;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using 纸片人.AttributeUsageFloder;
using 纸片人.InfoDataFolder;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.GpuPage
{
    [PageType("gpu", "置换液化")]
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class DisplacementLiquePage : Page
    {


        public DisplacementLiquePage()
        {
            this.InitializeComponent();

            Init();
            ConstructOperator();
        }
        LayerBitmapData bitmapData = new LayerBitmapData();

        CanvasRenderTarget mappingBitmap;
        CanvasRenderTarget sourceBitmap;
        Vector2 Position;

        private void Init()
        {
            CanvasControl.CreateResources += (s, e) =>
            {
                //e.TrackAsyncAction(bitmapData.LoadShadeCode(Shaders.ShaderType.LiquefactionMapping).AsAsyncAction());
            };

            selectPicture.Click += async (s, e) =>
            {
                await bitmapData.SelectOneBitmapAsync();
                if (bitmapData.oneBitmap != null)
                {
                    mappingBitmap = new CanvasRenderTarget(CanvasControl, bitmapData.oneBitmap.Size);
                    sourceBitmap = new CanvasRenderTarget(CanvasControl, bitmapData.oneBitmap.Size);
                    DrawMapping();
                    CanvasControl.Invalidate();
                }
            };

            CanvasControl.Draw += (s, e) =>
            {
                Function.DrawGridGraphics(e.DrawingSession);
                if (bitmapData != null && mappingBitmap != null)
                {

                    e.DrawingSession.DrawImage(new DisplacementMapEffect
                    {
                        Source = sourceBitmap,
                        Displacement = mappingBitmap,
                        XChannelSelect = EffectChannelSelect.Red,
                        YChannelSelect = EffectChannelSelect.Green,
                        Amount = 100,
                    });


                    //var width = (float)CanvasControl.ActualWidth;
                    //var height = (float)CanvasControl.ActualHeight * 0.65;
                    //var te = Function.CalcutateImageCenteredTransform(width, height, bitmapData.oneWidth, bitmapData.oneHeight);
                    //te.Source = bitmapData.oneBitmap;
                    //e.DrawingSession.DrawImage(bitmapData.oneBitmap);
                }

                //绘制原图
                if (bitmapData.oneBitmap != null)
                {
                    var width = (float)CanvasControl.ActualWidth * 0.25f;
                    var height = (float)CanvasControl.ActualHeight * 0.3;
                    var te = Function.CalcutateImageCenteredTransform(width, height, bitmapData.oneWidth, bitmapData.oneHeight);
                    te.Source = bitmapData.oneBitmap;
                    e.DrawingSession.DrawImage(te, 0, (float)CanvasControl.ActualHeight * 0.7f);
                }
                //绘制置换图
                if (mappingBitmap != null)
                {
                    var width = (float)CanvasControl.ActualWidth * 0.25f;
                    var height = (float)CanvasControl.ActualHeight * 0.3;
                    var te = Function.CalcutateImageCenteredTransform(width, height, bitmapData.oneWidth, bitmapData.oneHeight);
                    te.Source = mappingBitmap;
                    e.DrawingSession.DrawImage(te, width, (float)CanvasControl.ActualHeight * 0.7f);
                }

                e.DrawingSession.DrawCircle(this.Position, 122, Colors.Gray);
            };


        }


        void ConstructOperator()
        {
            Operator.Single_Start += (point, properties) =>
            {
                Position = point;
            };

            Operator.Single_Delta += (point, properties) =>
             {
                 //当前位置
                 var position = point;
                 float pressure = properties.Pressure;
                 //当前位置-开始位置
                 var vector = position - Position;
                 float length = vector.Length();
                 if (length < 2) return;

                 Vector2 unit = vector / length;
                 var color = unit.ToColor();
                 using (var ds = mappingBitmap.CreateDrawingSession())
                 {
                     ds.FillCircle(position, 80, new CanvasRadialGradientBrush(CanvasControl, color, Colors.Transparent)
                     {
                         Center = position,
                         RadiusX = 80,
                         RadiusY = 80,
                         Opacity = 0.4f,
                     });
                 }
                 this.Position = position;
                 CanvasControl.Invalidate();
             };

            Operator.Single_Complete += (point, properties) =>
            {
                if (sourceBitmap == null)
                    return;

                using (var ds = sourceBitmap.CreateDrawingSession())
                {
                    ds.Clear(Colors.Transparent);
                    ds.DrawImage(new DisplacementMapEffect
                    {
                        Source = bitmapData.oneBitmap,
                        Displacement = mappingBitmap,
                        XChannelSelect = EffectChannelSelect.Red,
                        YChannelSelect = EffectChannelSelect.Green,
                        Amount = 100
                    });
                }
                bitmapData.oneBitmap.CopyPixelsFromBitmap(sourceBitmap);
                DrawMapping();
                CanvasControl.Invalidate();
            };
        }


        /// <summary>
        /// 绘制法线图
        /// </summary>
        void DrawMapping()
        {
            if (mappingBitmap == null)
                return;
            using (var ds = mappingBitmap.CreateDrawingSession())
            {
                ds.Clear(Colors.Transparent);
                ds.FillRectangle(new Rect(0, 0, bitmapData.oneWidth, bitmapData.oneHeight), Color.FromArgb(255, 128, 128, 255));
            }
        }
    }
}
